package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("AdminOrderController")
@Slf4j
@Api(tags = "订单管理接口")
@RequestMapping("/admin/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 订单搜索接口
     *
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索接口")
    public Result<PageResult> OrderPageQuery(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("订单管理接口分页查询参数:{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.AdminPageQuery(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 各个状态的的订单数量统计
     *
     * @return
     */
    @ApiOperation("各个状态的的订单数量统计")
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> Statistics() {
        OrderStatisticsVO orderStatisticsVO = orderService.countStatus();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 根据订单id查询订单详情
     *
     * @param id
     * @return
     */
    @ApiOperation("查询订单详情")
    @GetMapping("/details/{id}")
    public Result<OrderVO> getDetailById(@PathVariable Long id) {
        OrderVO orderVO = orderService.getById(id);
        return Result.success(orderVO);
    }

    /**
     * 接单
     * @param ordersDTO
     * @return
     */
    @ApiOperation("接单")
    @PutMapping("/confirm")
    public Result confirmOrder(@RequestBody OrdersDTO ordersDTO) {
        log.info("要接单的对象为:{}", ordersDTO);
        orderService.UpdateStatus(ordersDTO);
        return Result.success();
    }

    /**
     * 拒单
     * @param ordersRejectionDTO
     * @return
     * @throws Exception
     */

    @ApiOperation("拒单")
    @PutMapping("/rejection")
    public Result Rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        log.info("拒单的信息为:{}", ordersRejectionDTO);
        orderService.Reject(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 取消订单
     * @param ordersCancelDTO
     * @return
     */

    @ApiOperation("取消订单")
    @PutMapping("/cancel")
    public Result CancelOrder(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception{
        log.info("取消订单的信息为:{}",ordersCancelDTO);
        orderService.cancel(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 派送订单
     * @param id
     * @return
     * @throws Exception
     */
    @ApiOperation("派送订单")
    @PutMapping("/delivery/{id}")
    public Result deliveryOrder(@PathVariable Long id) throws Exception{
        log.info("要派送的订单id为:{}",id);
        orderService.delivery(id);
        return Result.success();
    }
    @ApiOperation("完成订单")
    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable Long id) throws Exception {
        orderService.complete(id);
        return Result.success();
    }


}
